// Foundation for Sites by ZURB
// foundation.zurb.com
// Licensed under MIT Open Source
@import 'math';

$contrast-warnings: true !default;

////
/// @group functions
////

/// 检查 `$color` 的亮度
///
/// @param {Color} $color - 检查亮度的颜色
///
/// @returns {Number} `$color` 的亮度
@function color-luminance($color) {
  // Adapted from: https://github.com/LeaVerou/contrast-ratio/blob/gh-pages/color.js
  // Formula: http://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef
  $rgba: red($color), green($color), blue($color);
  $rgba2: ();

  @for $i from 1 through 3 {
    $rgb: nth($rgba, $i);
    $rgb: $rgb / 255;

    $rgb: if($rgb < 0.03928, $rgb / 12.92, pow(($rgb + 0.055) / 1.055, 2.4));

    $rgba2: append($rgba2, $rgb);
  }

  @return 0.2126 * nth($rgba2, 1) + 0.7152 * nth($rgba2, 2) + 0.0722 * nth($rgba2, 3);
}

/// 检查两种颜色的对比度
///
/// @param {Color} $color1 - 第一种颜色
/// @param {Color} $color2 - 第二种颜色
///
/// @returns {Number} 比较颜色的对比度
@function color-contrast($color1, $color2) {
  // Adapted from: https://github.com/LeaVerou/contrast-ratio/blob/gh-pages/color.js
  // Formula: http://www.w3.org/TR/2008/REC-WCAG20-20081211/#contrast-ratiodef
  $luminance1: color-luminance($color1) + 0.05;
  $luminance2: color-luminance($color2) + 0.05;
  $ratio: $luminance1 / $luminance2;

  @if $luminance2 > $luminance1 {
    $ratio: 1 / $ratio;
  }

  $ratio: round($ratio * 10) / 10;

  @return $ratio;
}

/// 检测 `$base` 的亮度，并返回对比度最大的 `$colors`（颜色列表）的颜色。
///
/// @param {Color} $base - 检查亮度的颜色
/// @param {List} $colors [($white, $black)] - 对比的颜色
/// @param {Number} $tolerance [$global-color-pick-contrast-tolerance] - 对比度容差
///
/// @returns {Color} 来自 `$colors`（颜色列表）的最高颜色对比度。
@function color-pick-contrast($base, $colors: ($white, $black), $tolerance: $global-color-pick-contrast-tolerance) {
  $contrast: color-contrast($base, nth($colors, 1));
  $best: nth($colors, 1);

  @for $i from 2 through length($colors) {
    $current-contrast: color-contrast($base, nth($colors, $i));
    @if ($current-contrast - $contrast > $tolerance) {
      $contrast: color-contrast($base, nth($colors, $i));
      $best: nth($colors, $i);
    }
  }

  @if ($contrast-warnings and $contrast < 3) {
    @warn "Contrast ratio of #{$best} on #{$base} is pretty bad, just #{$contrast}";
  }

  @return $best;
}

/// 如果颜色较浅则将颜色缩放为较深颜色，如果颜色较深则将颜色变浅。使用此功能可以调整适合其亮度的颜色
///
/// @param {Color} $color - 要缩放的颜色
/// @param {Percentage} $scale [5%] - 上调或下调的量
/// @param {Percentage} $threshold [40%] - 要检查的亮度的阈值
///
/// @returns {Color} 缩放后的颜色
@function smart-scale($color, $scale: 5%, $threshold: 40%) {
  @if lightness($color) > $threshold {
    $scale: -$scale;
  }
  @return scale-color($color, $lightness: $scale);
}

/// 从 foundation 调色板中获取颜色
///
/// @param {key} 颜色键
///
/// @returns {Color} 颜色
@function get-color($key) {
  @if map-has-key($foundation-palette, $key) {
    @return map-get($foundation-palette, $key);
  } @else {
    @error 'given $key is not available in $foundation-palette';
  }
}

/// 将 `$foundation-palette` Map 中的颜色转换为变量，例如 `$primary-color` 和 `$secondary-color`。在配置文件的全局部分下方调用此 mixin 以正确迁移代码库
@mixin add-foundation-colors() {
  @if map-has-key($foundation-palette, primary) {
    $primary-color: map-get($foundation-palette, primary) !global;
  } @else {
    $primary-color: #1779ba !global;
  }
  @if map-has-key($foundation-palette, secondary) {
    $secondary-color: map-get($foundation-palette, secondary) !global;
  } @else {
    $secondary-color: #767676 !global;
  }
  @if map-has-key($foundation-palette, success) {
    $success-color: map-get($foundation-palette, success) !global;
  } @else {
    $success-color: #3adb76 !global;
  }
  @if map-has-key($foundation-palette, warning) {
    $warning-color: map-get($foundation-palette, warning) !global;
  } @else {
    $warning-color: #ffae00 !global;
  }
  @if map-has-key($foundation-palette, alert) {
    $alert-color: map-get($foundation-palette, alert) !global;
  } @else {
    $alert-color: #cc4b37 !global;
  }
}
